package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.GoodsFree;
import com.eco.fanliapp.service.IAppGoodsService;
import com.eco.fanliapp.service.IGoodsFreeService;
import com.eco.fanliapp.service.ITaoBaoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/web/goodsFree")
public class GoodsFreeController extends BaseController {

    @Autowired
    private IGoodsFreeService goodsFreeService;
    @Autowired
    private ITaoBaoService taoBaoService;

    @Autowired
    private IAppGoodsService goodsService ;
    @SysLogAnnotation(value = "免单商品添加",userType = 2)
    @PostMapping("/addGoodsFree")
    public ServerResponse addGoodsFree(@RequestBody GoodsFree goodsFreeInfo) {
        if(getAuthentication().getRoleId() != 0){
            goodsFreeInfo.setCompanyId(getAuthentication().getCompanyId());
        }
        return goodsFreeService.addGoodsFreeByTaoBao(goodsFreeInfo);
    }
    @SysLogAnnotation(value = "免单商品更新",userType = 2)
    @RequestMapping(method = RequestMethod.POST,value = "/updateByGoodsFree")
    public ServerResponse updateByGoodsFree(@RequestBody GoodsFree goodsFree) {
        if(getAuthentication().getRoleId() != 0){
            goodsFree.setCompanyId(getAuthentication().getCompanyId());
        }
        return goodsFreeService.updateByGoodsFree(goodsFree);
    }

    @GetMapping("/selectById")
    public GoodsFree selectByGoodsId(String goodsId) {


        return goodsFreeService.selectByGoodsId(goodsId);

    }

    @GetMapping("/selectByPlatform")
    public ServerResponse<List> selectByPlatform(Integer goodsPlatform) {

        return goodsFreeService.selectByPlatform(goodsPlatform);
    }

    @GetMapping("/findAll")
    public ServerResponse<PageInfo> findAll(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "5") Integer pageSize) {
        Integer companyId = -1;
        if(getAuthentication().getRoleId() != 0){
            companyId = getAuthentication().getCompanyId();
        }
        return goodsFreeService.findAll(companyId,currentPage,pageSize);
    }

    @SysLogAnnotation(value = "删除免单商品",userType = 2)
    @GetMapping("/deleteByGoodsId")
    public ServerResponse deleteByGoodsId(Integer id) {

        return goodsFreeService.deleteByGoodsId(id);
    }
    @SysLogAnnotation(value = "更新免单商品",userType = 2)
    @PostMapping("/updateByGoodsId")
    public ServerResponse<List> updateByGoodsIdSelective(@RequestBody GoodsFree goodsFree) {

        return goodsFreeService.updateByGoodsIdSelective(goodsFree);
    }

    @SysLogAnnotation(value = "更新免单商品",userType = 2)
    @GetMapping("/updateGoodsFreeStageByGoodsId")
    public ServerResponse<PageInfo> updateGoodsFreeStageByGoodsId(String goodsId , Integer goodsFreeStage) {
        return goodsFreeService.updateGoodsFreeStageByGoodsId(goodsId,goodsFreeStage);
    }

    /**
     *
     * @param goodsIdOrGoodsName
     * @return
     */
    @GetMapping("/selectByGoodsIdOrGoodsName")
    public ServerResponse<List> selectByGoodsIdOrGoodsName(String goodsIdOrGoodsName) {

        return goodsFreeService.selectByGoodsIdOrGoodsName(goodsIdOrGoodsName);
    }

    /**
     * 根据平台或者关键字查询
     * @param goodsPlatform
     * @param keyWord
     * @return
     */
    @GetMapping("/findGoodsFreeByKeyWord")
    public ServerResponse<PageInfo> findGoodsFreeByKeyWord(Integer goodsPlatform , String keyWord, @RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "5") Integer pageSize) {
        Integer companyId = -1;
        if(getAuthentication().getRoleId() != 0){
            companyId = getAuthentication().getCompanyId();
        }
        return goodsFreeService.selectByKeyWord(companyId,goodsPlatform,keyWord,currentPage,pageSize);
    }

    /**
     * 修改免单序号
     */
    @SysLogAnnotation(value = "修改免单序号",userType = 2)
    @RequestMapping(method = RequestMethod.POST,value = "/updateSerial")
    public ServerResponse updateSerial(@RequestBody HashMap<String, Object> params){
        return goodsFreeService.updateSerial(params);
    }

}
